<?php

require "includes/class.template.php";
$template = new verySimpleTemplate();

switch( @$_POST['action'] )
{
	case 'ip':
	if( $_POST['domain'] ) { ip( $_POST['domain'] ); } else { def(); } break;

	case 'mb':
	if( $_POST['mobile'] ) { mb( $_POST['mobile'] ); } else { def(); } break;

	case 'id':
	if( $_POST['idcard'] ) { id( $_POST['idcard'] ); } else { def(); } break;

	default: def(); break;
}

function ip( $c )
{
	global $template;
	require 'includes/class.ip2location.php';

	$o = new Ip2Location;
	$p = "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";

	if ( preg_match( "/[a-zA-Z\-_]+/si", $c ) )
	{
		$i = gethostbyname( $c );
		if ( eregi( $p, $i ) )
		{
			$o->qqwry( $i );
			$l = str_replace( 'CZ88.NET', '', ($o->Country . $o->Local) );
			$arrayVar = array();
			$template->deal( 'template/header.tpl' );
			$h = $template->template;
			$template->deal( 'template/idxBody.tpl' );
			$b = $template->template;
			$arrayVar = array ( 
								'DOMAIN'	=> $c,
								'DOMAIN_IP' => $i,
								'LOCALTION' => $l
							  );
			$template->deal( 'template/domainLocationRender.tpl', $arrayVar );
			$r = $template->template;
			$template->deal( 'template/footer.tpl' );
			$f = $template->template;
			echo $h . $r . $b . $f;
		}
		else
		{
			error( '对不起,您输入的域名信息有误,请重新输入.' );
		}
	}
	else
	{
		if ( eregi( $p, $c ) )
		{
			$o->qqwry( $c );
			$l = str_replace( 'CZ88.NET', '', ($o->Country . $o->Local) );
			$arrayVar = array();
			$template->deal( 'template/header.tpl' );
			$h = $template->template;
			$template->deal( 'template/idxBody.tpl' );
			$b = $template->template;
			if( @$_GET['ipwhois'] )
			{
				$arrayVar = array ( 
									'IP'  => @$_GET['ipwhois'],
									'LOCALTION' => $l
								);
				$template->deal( 'template/ipLocationWhoisRender.tpl', $arrayVar );
				$r = $template->template;
				$template->deal( 'template/footer.tpl' );
				$f = $template->template;
				echo $h . $r . $b . $f;
			}
			else
			{
				$arrayVar = array ( 
									'IP'  => $c,
									'LOCALTION' => $l
								);
				$template->deal( 'template/ipLocationRender.tpl', $arrayVar );
				$r = $template->template;
				$template->deal( 'template/footer.tpl' );
				$f = $template->template;
				echo $h . $r . $b . $f;
			}
		}
		else
		{
			error( '对不起,您输入的IP信息有误,请重新输入.' );
		}
	}


}

function mb( $c )
{
	global $template;
	define( 'MOBI_PATH', './data/mobile/' );

	$fa = array('130','131','132','133','134','135','136','137','138','139','159');
	if( !is_numeric( $c ) or strlen( $c ) < 7 or strlen( $c ) > 11 or (substr( $c, 0, 2 ) != '13' and substr( $c, 0, 3 ) != '159') or !in_array( substr( $c, 0, 3 ), $fa ) )
	{
		if ( !is_numeric( $c ) )
		{
			error( '对不起,查询的手机号码只能为数字,请重新输入.' );
		}
		elseif ( strlen( $c ) < 7 or strlen( $c ) > 11 )
		{
			error( '对不起,查询的手机号码只能在7位到11位之间,请重新输入.' );
		}
		elseif ( substr( $c, 0, 2 ) != '13' and substr( $c, 0, 3 ) != '159' )
		{
			error( '对不起,查询的手机号码只能是13或159开头的,请重新输入.' );
		}
		elseif ( !in_array( substr( $c, 0, 3 ), $fa ) )
		{
			error( '对不起,您查询的手机号码网段不正确,请重新输入.' );
		}
		else
		{
			error( '对不起,您输入的手机号码信息有误,请重新输入.' );
		}
	}
	else
	{
		$p  = array();
		$se = substr( $c, 0, 7 );
		$fn = 'data/mobile/' . substr( $c, 0, 3 ) . '/' . substr( $c, 3, 1 ) . '.dat';		
		$fp = @fopen( $fn, 'r' );
		while( $line = @fgets( $fp, 1024 ) )
		{
			$p = explode( '|', $line );
			if( $se == $p[0])
			{
					$lt = explode( ' ', $p[1] );
					if( count( $lt ) == 2 )
					{
						$l = $lt[0] == $lt[1] ? $lt[0] : $lt[0] . $lt[1];
					}
					else
					{
						$l = $lt[0];
					}
					$template->deal( 'template/header.tpl' );
					$h = $template->template;
					$template->deal( 'template/idxBody.tpl' );
					$b = $template->template;
					$tp = $p[2];
					$arrayVar = array (
										'MOBILE'	=> $c,
										'LOCATION'	=> $l,
										'TYPE'		=> $tp
									  );
					$template->deal( 'template/mobileRender.tpl', $arrayVar );
					$r = $template->template;
					$template->deal( 'template/footer.tpl' );
					$f = $template->template;
					echo $h . $r . $b . $f;
					break;
				
			}
		}
		@fclose( $fp );
		if( !$p )
		{
			error( '对不起,此号码数据暂时没有收录.' );
		}
	}
}

function id( $c )
{
	global $template, $cantons;

	require "data/idcard.data.php";
	if( !is_numeric( substr( $c, 0, 17 ) ) )
	{
		error( '对不起,您输入的身份证号码信息有误,请重新输入.' );
	}
	else
	{
		$l = strlen( $c );
		$s = substr( $c, 0, 6 );
		switch ( $l )
		{
			case 15:
			$y = '19' . substr( $c, 6, 2 );
			$m = substr( $c, 8, 2 );
			$d = substr( $c, 10, 2);
			if ( !checkdate( $m, $d, $y ) )
			{
				error( '对不起,您输入的身份证生日信息有误,请重新输入.' );
				break;
			}
			else
			{
				$sigma = 0;
				$st = $s . $y . $m . $d . substr( $c, 12, 3 );
				$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
				$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
				for ($i = 0; $i < 17; $i++) 
				{
					$b = (int) $st{$i};
					$w = $wi[$i];
					$sigma += $b * $w; 
				}
				$n  = $sigma % 11;
				$ck = $ai[$n];
				$u  = $st . $ck;
			}
			$ml = array( 1, 3, 5, 7, 9 );
			$g  = in_array( substr( $c, -1 ), $ml ) ? '男' : '女';
			$p  = ( (int) substr( $c, 0, 3 ) * 1000 );
			$t  = ( (int) substr( $c, 0, 4 ) * 100 );
			$o  = (int) substr( $c, 0, 6 );
			if ( @!$cantons[$o] )
			{
				$a = '数据暂时没有收录';
			}
			else
			{
				$a = $cantons[$p] . $cantons[$t] . $cantons[$o];
			}
			$z = $y .'年' . $m . '月' . $d . '日';
			$template->deal( 'template/header.tpl' );
			$h = $template->template;
			$template->deal( 'template/idxBody.tpl' );
			$v = $template->template;
			$arrayVar = array (
								'IDCARD'	=> $c,
								'GENDER'	=> $g,
								'BIRTH'		=> $z,
								'LOCALTION' => $a,
								'UPDATE'	=> $u
							   );
			$template->deal( 'template/idcard_15_Render.tpl', $arrayVar );
			$r = $template->template;
			$template->deal( 'template/footer.tpl' );
			$f = $template->template;
			echo $h . $r . $v . $f;
			break;

			case 18:
			$y = substr( $c, 6,  4 );
			$m = substr( $c, 10, 2 );
			$d = substr( $c, 12, 2 );
			if ( !checkdate( $m, $d, $y ) )
			{
				error( '对不起,您输入的身份证生日信息有误,请重新输入.' );
				break;
			}
			else
			{
				$sigma = 0;
				$st = $s . $y . $m . $d . substr( $c, 14, 3 );
				$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
				$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
				for ( $i = 0; $i < 17; $i++ ) 
				{
					$b = (int) $st{$i};
					$w = $wi[$i];
					$sigma += $b * $w; 
				}
				$n  = $sigma % 11;
				$ck = $ai[$n];
				if ( ( substr( $c, -1 ) ) != $ck )
				{
					error( '对不起,您输入的身份证号码无效,请查正后重新输入.' );
					break;
				}
				$ml = array( 1, 3, 5, 7, 9 );
				$g  = in_array( substr( $c, -2, -1 ), $ml ) ? '男' : '女';
				$p  = ( (int) substr( $c, 0, 3 ) * 1000 );
				$t  = ( (int) substr( $c, 0, 4 ) * 100 );
				$o  = (int) substr( $c, 0, 6 );
				if ( @!$cantons[$o] )
				{
					$a = '数据暂时没有收录';
				}
				else
				{
					$a = $cantons[$p] . $cantons[$t] . $cantons[$o];
				}
				$z = $y .'年' . $m . '月' . $d . '日';
				$template->deal( 'template/header.tpl' );
				$h = $template->template;
				$template->deal( 'template/idxBody.tpl' );
				$v = $template->template;
				$arrayVar = array (
									'IDCARD'	=> $c,
									'GENDER'	=> $g,
									'BIRTH'		=> $z,
									'LOCALTION' => $a
								  );
				$template->deal( 'template/idcard_18_Render.tpl', $arrayVar );
				$r = $template->template;
				$template->deal( 'template/footer.tpl' );
				$f = $template->template;
				echo $h . $r . $v . $f;
			}
			break;

			default:
			error( '对不起,您输入的身份证号码信息有误,请重新输入.' );
			break;
		}
	}
}

function def()
{
	global $template;

	require 'includes/class.ip2location.php';
	$o = new Ip2Location();

	if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
	{
		$c = $_SERVER['HTTP_CLIENT_IP'];
	}
	elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
	{
		$c = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	elseif ( !empty( $_SERVER['REMOTE_ADDR'] ) )
	{
		$c = $_SERVER['REMOTE_ADDR'];
	}
	else
	{
		$c = '';
	}

	$c = trim( $c );

	if( $c )
	{
		$o->qqwry( $c );
		$l = str_replace( 'CZ88.NET', '', ($o->Country . $o->Local) );
	}
	else
	{
		$c = '未知';
		$l = '未知';
	}
	if( !$l ) $l = '未知';

	$arrayVar = array();
	$template->deal( 'template/header.tpl' );
	$h = $template->template;
	$template->deal( 'template/idxBody.tpl' );
	$b = $template->template;
	if( @$_GET['ipwhois'] )
	{
		$arrayVar = array ( 
							'LOCAL_IP'  => @$_GET['ipwhois'],
							'LOCALTION' => $l
						);
		$template->deal( 'template/ipLocalWhoisRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $r . $b . $f;
	}
	else
	{
		$arrayVar = array ( 
							'LOCAL_IP'  => $c,
							'LOCALTION' => $l
						);
		$template->deal( 'template/ipLocalRender.tpl', $arrayVar );
		$r = $template->template;
		$template->deal( 'template/footer.tpl' );
		$f = $template->template;
		echo $h . $r . $b . $f;
	}
}

function error( $e )
{
	global $template;
	$arrayVar = array();
	$template->deal( 'template/header.tpl' );
	$h = $template->template;
	$template->deal( 'template/idxBody.tpl' );
	$b = $template->template;
	$arrayVar = array ( 
						'ERROR'  => $e
					  );
	$template->deal( 'template/errorRender.tpl', $arrayVar );
	$r = $template->template;
	$template->deal( 'template/footer.tpl' );
	$f = $template->template;
	echo $h . $r . $b . $f;
}
?>